// 显示/隐藏错误窗口
function showError(inputId, message) {
    const errorWindow = document.getElementById(inputId + '-error');
    errorWindow.textContent = message;
    errorWindow.style.display = 'block';
}

function hideError(inputId) {
    const errorWindow = document.getElementById(inputId + '-error');
    errorWindow.style.display = 'none';
}


document.getElementById('username').addEventListener('input', function () {
    const name = document.getElementById('username');
    const usernameValue = name.value.trim();
    if (usernameValue === '') {
        showError('username', '用户名不能为空！');
    } else if (usernameValue.length < 5 || usernameValue.length > 20) {
        showError('username', '用户名长度必须在 5 到 20 个字符之间！');
    } else {
        hideError('username');
    }
});

document.getElementById('email').addEventListener('input', function () {
    const email = document.getElementById('email');
    const emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,6}$/;

    if (email.value.trim() === '') {
        showError('email', '邮箱不能为空！');
    } else if (!emailPattern.test(email.value.trim())) {
        showError('email', '请输入一个正确的邮箱');
    } else {
        hideError('email');
    }
});

document.getElementById('password').addEventListener('input', function () {
    const password = document.getElementById('password');
    const passwordValue = password.value.trim()
    if (passwordValue === '') {
        showError('password', '密码不能为空！');
    } else if (passwordValue.length < 5 || passwordValue.length > 20) {
        showError('password', '密码长度必须在 5 到 20 个字符之间！');
    } else {
        hideError('password');
    }
});

document.getElementById('confirm_password').addEventListener('input', function () {
    checkPasswordsMatch();
});
function checkPasswordsMatch() {
    const password = document.getElementById('password').value.trim();
    const confirmPassword = document.getElementById('confirm_password').value.trim();

    if (password !== confirmPassword) {
        showError('confirm_password', '密码与确认密码不一致！');
    } else {
        hideError('confirm_password');
    }
}